package server_init

import (
	"context"
	"github.com/gin-gonic/gin"
	"go_server/zinx/zlog"
	"net/http"
	"os"
	"os/signal"
	"syscall"
)

var httpServer *http.Server

func Start(serverAddr string) {
	//gin.SetMode(gin.ReleaseMode)
	//gin.DefaultWriter = io.Discard

	engine := gin.Default()

	httpServer = &http.Server{
		Addr:    serverAddr,
		Handler: engine,
	}

	go func() {
		if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			zlog.Panicf("中心服启动失败!!!, 错误为: %v", err)
		}
	}()

	zlog.Infof("中心服启动成功!!!监听地址为: %s", httpServer.Addr)

	c := make(chan os.Signal)
	signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
	sig := <- c
	zlog.Infof("中心服收到中断信号, 信号 = %v, 服务器即将关闭!!!", sig)
}

func Stop() {
	if err := httpServer.Shutdown(context.Background()); err != nil {
		zlog.Panicf("中心服关闭失败!!!,错误: %v", err)
	}

	zlog.Info("中心服关闭成功!!!")
}
